package com.example.city_home_app.Activity;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.transition.Explode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.city_home_app.Bean.Down_Login;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {


    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.bt_go)
    Button btGo;
    @BindView(R.id.cv)
    CardView cv;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.tv_login_forgot)
    TextView tvLoginForgot;

    private String LoginName;
    private String LoginPassword;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;


    public static LoginActivity instance;

    public FloatingActionButton getFab() {
        return fab;
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        instance = this;

    }

    @Override
    protected void initData() {
        super.initData();
        sharedPreferences = getSharedPreferences("Logininfo", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

    }


    @OnClick({R.id.bt_go, R.id.fab,R.id.tv_login_forgot})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_go:
                login();
                break;
            case R.id.fab:
                getWindow().setExitTransition(null);
                getWindow().setEnterTransition(null);
                ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(LoginActivity.this, fab, fab.getTransitionName());
                startActivity(new Intent(LoginActivity.this, RegActivity.class), options.toBundle());
                break;
            case R.id.tv_login_forgot:
                getWindow().setExitTransition(null);
                getWindow().setEnterTransition(null);
                ActivityOptions options2 = ActivityOptions.makeSceneTransitionAnimation(LoginActivity.this, fab, fab.getTransitionName());
                startActivity(new Intent(LoginActivity.this, ForgotActivity.class), options2.toBundle());
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        fab.setVisibility(View.GONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        fab.setVisibility(View.VISIBLE);
    }

    private RetrofitListener<Down_Login> loginRetrofitListener = new RetrofitListener<Down_Login>() {
        @Override
        public void onResponse(Down_Login listener) {
            if (listener.getMessage().equals("success")) {
                BaseActivity.setUserinfo(listener);
                editor.putString("LoginName", LoginName);
                editor.putString("LoginPassword", LoginPassword);
                editor.commit();
                Explode explode = new Explode();
                explode.setDuration(500);
                getWindow().setExitTransition(explode);
                getWindow().setEnterTransition(explode);
                ActivityOptionsCompat oc2 = ActivityOptionsCompat.makeSceneTransitionAnimation(LoginActivity.this);
                Intent i2 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(i2, oc2.toBundle());
                finish();
            }else{
                UIUtils.showToast(LoginActivity.this, listener.getMessage(), UIUtils.LENGTH_SHORT);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void login() {
        if (!TextUtils.isEmpty(etUsername.getText()) && !TextUtils.isEmpty(etPassword.getText())) {
            ApiModel model = new ApiModel();
            LoginName = etUsername.getText().toString();
            LoginPassword = etPassword.getText().toString();
            model.login(LoginName, LoginPassword, loginRetrofitListener);
        } else {
            UIUtils.showToast(this, "请输入完整信息", UIUtils.LENGTH_SHORT);
        }
    }


}
